package Excel;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSON;

import Excel.entity.TestEntity;

public class EasyExcelDemo {

	
	private static List<TestEntity> data() {
//        List<TestEntity> list = ListUtils.newArrayList();
		List<TestEntity> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
        	TestEntity entity = new TestEntity();
        	entity.setStringTitile("字符串" + i);
        	entity.setDateTitle(new Date());
        	entity.setDoubleTitle(0.213123d + i);
            list.add(entity);
        }
        return list;
    }
	
	public static void simpleWrite() {

        // 注意 simpleWrite在数据量不大的情况下可以使用（5000以内，具体也要看实际情况），数据量大参照 重复多次写入
        String fileName = "D:\\workspace\\projectBackUps\\work\\easyTest.xlsx";
        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        // 如果这里想使用03 则 传入excelType参数即可
        com.alibaba.excel.EasyExcel.write(fileName, TestEntity.class)
                .sheet("模板")
                .doWrite(() -> {
                    return EasyExcelDemo.data();
                });
	}
	
    public static void writeACulloum() {

        String fileName = "D:/workspace/projectBackUps/work/easyTest.xlsx";
    	// 根据用户传入字段 假设我们只要导出 date
        Set<String> includeColumnFiledNames = new HashSet<String>();
        includeColumnFiledNames.add("doubleTitle");
        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        EasyExcel.write(fileName, TestEntity.class).includeColumnFiledNames(includeColumnFiledNames)
        	.sheet("模板")
            .doWrite(EasyExcelDemo.data());
    }
    
	public static void writeDataToTable() {
		 String fileName = "D:\\workspace\\projectBackUps\\work\\easyTest1.xlsx";	 
		 com.alibaba.excel.EasyExcel.write(fileName, TestEntity.class)
		 	.sheet("sheetTableName")
		 	.doWrite(EasyExcelDemo.data());
		 List<TestEntity> list  = EasyExcelDemo.data();
		 try(ExcelWriter ew = com.alibaba.excel.EasyExcel.write(fileName, TestEntity.class).build()) {
			 for (int i = 0; i < 10; i++) {
				WriteSheet writeSheet = com.alibaba.excel.EasyExcel.writerSheet(i, "table"+i).build();
				ew.write(list, writeSheet);
			}
		 }
	}

    public static void simpleRead() {
        // 写法1：JDK8+ ,不用额外写一个TestEntityListener
        String fileName = "D:/workspace/projectBackUps/work/easyTest.xlsx";
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        // 这里每次会读取3000条数据 然后返回过来 直接调用使用数据就行
        com.alibaba.excel.EasyExcel.read(fileName, TestEntity.class, new PageReadListener<TestEntity>(dataList -> {
        	
        	System.out.println(dataList.toString());
        	System.out.println("===========================");
        	
            for (TestEntity demoData : dataList) {
                System.out.println(JSON.toJSONString(demoData));
            }
        })).sheet().doRead();
    }
    

	public static void main(String[] args) {

//		SimpleDateFormat simpleDateFormat  = new SimpleDateFormat("yyyy-MM-dd HH:mm:sss");       
//		List<TestEntity> list =data();
//		list.forEach(System.out::println);
		
		System.out.println("===============================");
//		simpleWrite();
//		writeDataToTable();

		writeACulloum();
		simpleRead();
	}

}
